<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\AdminAdmin;
use app\common\facade\admin\RulesAdmin;
use think\exception\ValidateException;

class Rules extends BaseController
{
    /**
     * 获取权限列表
     */
    public function rulesAlllist()
    {
        try {
            $data = AdminAdmin::rulesAlllist();
        } catch (\Exception $e) 
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 根据id获取权限信息
     */

     public function rulesInfo($id)
     {
        $param = [
            'id' => $id
        ];
        try {
            $data = RulesAdmin::rulesInfo($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 权限信息编辑保存
     */
    public function rulesAdd()
    {
        $param = $this->request->post([
            'id',
            'pid',
            'auth_name',
            'path',
            'path_url',
            'level',
            'menu',
            'icon'
        ]);

        try {            
            $this->validate($param, 'app\common\validate\Rules.addsave');            
            $data = RulesAdmin::ruleAddEditSave($param);          
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);          
    }

    /**
     * 权限删除接口
     */
    public function rulesDelete($id)
    {
        $param = [
            'id' => $id
        ];

        try {
            $data = RulesAdmin::rulesDelete($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

    /**
     * 权限删除接口
     */
    public function rulesDeletes()
    {
        $param = $this->request->post([
            'idstr'
        ]);

        try {
            $data = RulesAdmin::rulesDeletes($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }
}